class Bill {
  constructor(id, user_id, category_id, title, amount, type, bill_date, description, payment_method, account_id, transfer_to_account_id, created_at, updated_at) {
    this.id = id;
    this.user_id = user_id;
    this.category_id = category_id;
    this.title = title;
    this.amount = parseFloat(amount);
    this.type = type;
    this.bill_date = bill_date instanceof Date ? bill_date : new Date(bill_date);
    this.description = description;
    this.payment_method = payment_method;
    this.account_id = account_id;
    this.transfer_to_account_id = transfer_to_account_id; // 新增字段
    this.created_at = created_at instanceof Date ? created_at : new Date(created_at);
    this.updated_at = updated_at instanceof Date ? updated_at : new Date(updated_at);
  }

  // Create a Bill instance from a database row
  static fromRow(row) {
    return new Bill(
      row.id,
      row.user_id,
      row.category_id,
      row.title,
      row.amount,
      row.type,
      row.bill_date,
      row.description,
      row.payment_method,
      row.account_id,
      row.transfer_to_account_id, // 新增字段
      row.created_at,
      row.updated_at
    );
  }

  // Convert Bill instance to JSON
  toJSON() {
    return {
      id: this.id,
      user_id: this.user_id,
      category_id: this.category_id,
      title: this.title,
      amount: this.amount,
      type: this.type,
      bill_date: this.bill_date.toISOString(),
      description: this.description,
      payment_method: this.payment_method,
      account_id: this.account_id,
      transfer_to_account_id: this.transfer_to_account_id, // 新增字段
      created_at: this.created_at.toISOString(),
      updated_at: this.updated_at.toISOString()
    };
  }
}

module.exports = Bill;